<?php
namespace Home\Biz;
use Home\Biz\StringBiz;

class SkuBiz{
	
	public static function caculateTotalPage($totalCount, $pageSize)
	{
	
		$count = intval($totalCount / $pageSize);
	
		$countLeft = $totalCount % $pageSize;
	
		if ($countLeft != 0) {
			$count = $count + 1;
		}
	
		return $count;
	}
	
	
	public static function updateSku($sku_en){
		$split_en = array(
			'invoice_title' => I('invoice_title'),
			'referent_id' => I('referent_id'),
			'invoice_name' => $sku_en['sku_name'],
			'sku_id' => $sku_en['sku_id']
		);
		$error = self::validateSku($sku_en,$split_en);
		if(!empty($error)){
			 
			return $error;
		}
		
		$update_style_en= array(
		
				'style_name'	=> $sku_en['style_name'] ,
				'sku_channel'	=> $sku_en['sku_channel'] ,
				'sku_channel_name'	=> $sku_en['sku_channel_name'],
				'market_price'	=> $sku_en['market_price'] ,
				'shop_price'	=> $sku_en['market_price'] ,
				'sale_min_price'	=> $sku_en['sale_price'],
				'sale_max_price'	=> $sku_en['sale_price'] ,
				'is_real'	=> 1 ,
				'status'	=> 1 ,
				'update_user'	=> "whaleyFz.admin skuBiz.upateSku",
				'update_date'	=> date("y-m-d H:i:s", time())
		);
		
			M('sku')->where("sku_id ='%s'", $sku_en['sku_id'])->save($sku_en);
			M('style')->where("style_id='%s'",$sku_en['style_id'])->save($update_style_en);
			//更新时操作产品扩展信息数据
			$product_split = self::getSplitInfoBySku($sku_en['sku_id']);
			//若以前没有添加过 添加 否则更新
			if(empty($product_split)){
				self::addProductSplit($split_en,$sku_en);
			}else{
				self::updateProductSplit($split_en,$sku_en,$product_split['id']);
			}
	}
	
	
	
	public static function validateSku($sku_en,$split_en){
		
		
		if(empty($sku_en['product_name'])){
		
			return "产品名称不能为空!";
			
		}	
		if(empty($sku_en['product_id'])){
			
			return "产品编号不能为空!";					
		}
		if(empty($sku_en['style_sn'])){

			return "产品编码不能为空!";
		}
		if(empty($sku_en['material_no'])){
				
			return "产品物料号不能为空!";
		}
		if(empty($sku_en['sku_name'])){
		
			return "sku名称不能为空!";
		}
		if(empty($sku_en['sku_channel'])){
		
			return "sku渠道不能为空!";
		}
		
		
		if(!empty($sku_en['market_price'])){
		
			if(!is_numeric($sku_en['market_price'])){
				
				return "市场价必须为数字类型!";
			}	
			
			
			if($sku_en['market_price'] <0 ){
				return  "市场价必须大于等于0";
			}
		}
		
		
		 if(!empty($sku_en['sale_price'])){
		
			if(!is_numeric($sku_en['sale_price'])){
				
				return "单价必须为数字类型!";
			}
			if($sku_en['sale_price'] <0 ){
				return  "单价必须大于等于0";
			}
				
					
			
		}else{
			
			if($sku_en['sale_price'] != 0  && empty($sku_en['sale_price'])  ){
				
				return "单价不能为空!";
			}
						
		} 
		
		 
		
		if(!empty($sku_en['dealer_min_limit_price'])){
			
			if(!is_numeric($sku_en['dealer_min_limit_price'])){
				return "经销商渠道最低限价必须为数字类型!";
			}	
			if($sku_en['dealer_min_limit_price'] <0 ){
				return  "经销商渠道最低限价必须大于等于0";
			}
		}else{
			
			if($sku_en['sku_channel']== '8'){
				
			 if($sku_en['dealer_min_limit_price']!=0 && empty($sku_en['dealer_min_limit_price'])){	
				return "经销商渠道最低限价不能为空!";
			 }
			}
			
			
			
		}
		if(!empty($sku_en['person_commission_amount'])){
		
			if(!is_numeric($sku_en['person_commission_amount'])){
				return "个人提成金额必须为数字类型!";
			}
			
			if($sku_en['person_commission_amount'] <0 ){
				return  "个人提成金额必须大于等于0";
			}
			
		}
		if(!empty($sku_en['dealer_commission_amount'])){
		    if(!is_numeric($sku_en['dealer_commission_amount'])){
		    	
		    	return "经销商提成金额必须为数字类型!";
		    }	
		    if($sku_en['dealer_commission_amount'] <0 ){
		    	return  "经销商提成金额必须大于等于0";
		    }
			
		}
		if(empty($split_en['invoice_title'])){
			return "开票名称不能为空!";
		}
		
		return '';
	}
	
	
	public static function addSku($sku_en){
	   $split_en = array(
	   		'invoice_title' => I('invoice_title'),
	   		'referent_id' => I('referent_id'),
	   		'invoice_name' => $sku_en['sku_name'],
	   		'sku_id' => $sku_en['sku_id']
	   );
	   $error = self::validateSku($sku_en,$split_en);
	   if(!empty($error)){
	    	return $error;
	   }	    
	   $is_sku_en = M('sku')->where("sku_id='%s'",$sku_en['sku_id'])->find();	
	   if(!empty($is_sku_en)){
	   		return "sku产品已经存在，请检查";
	   }
	   $is_style_en = M('style')->where("style_id='%s'",$sku_en['style_id'])->find();		   
	   if(!empty($is_style_en)){
	   		return "style商品已经存在，请检查";
	   }
	   $add_style_en= array(
   			'style_id'	=> $sku_en['style_id'],
   			'style_name'	=> $sku_en['style_name'] ,
   			'sku_channel'	=> $sku_en['sku_channel'] ,
   			'sku_channel_name'	=> $sku_en['sku_channel_name'],
   			'market_price'	=> $sku_en['market_price'] ,
   			'shop_price'	=> $sku_en['market_price'] ,
   			'sale_min_price'	=> $sku_en['sale_price'],
   			'sale_max_price'	=> $sku_en['sale_price'] ,
   			'is_real'	=> 1 ,
   			'status'	=> 1 ,
   			'create_user'	=> "whaleyFz.admin skuBiz.addSku",
   			'create_date'	=> date("y-m-d H:i:s", time())
	   	);
	   	
	   	M('sku')->add($sku_en);
	   	M('style')->add($add_style_en);
	   	//添加产品扩展信息
	   	self::addProductSplit($split_en,$sku_en);
	   	
	}
	
	
	public static function querySkuByPara($sku_sn, $sku_name , $style_name,$product_name, $sku_channel,$status,$pageSize,$index ){
		
		if (!empty ($sku_sn)) {
			$condition_array['sku_sn'] = array('like', "%" . $sku_sn . "%");
		}
		
		if (!empty ($sku_name)) {
			$condition_array['sku_name'] = array('like', "%" . $sku_name . "%");
		}
		
		if (!empty ($product_name)) {
			$condition_array['product_name'] = array('like', "%" . $product_name . "%");
		}
		if (!empty ($style_name)) {
			$condition_array['style_name'] = array('like', "%" . $style_name . "%");
		}
		if (!empty ($sku_channel)) {
			$condition_array['sku_channel'] = array('eq',  $sku_channel );
		}
		
		if (!empty ($status)) {
			if($status =="1"){
				$condition_array['status'] = array('eq', $status);
			}
		}else{
			
			$condition_array['status'] = array('eq', "0");
		}
			
	
		$lst_skus = M('sku')->field()->where($condition_array)->order('create_date asc')->page($index,$pageSize)->select();
	
		foreach ($lst_skus as $key => $lst_sku){
			
			$lst_skus[$key]['sku_channel_text']=StringBiz::getSkuChannelRevenueStatusText($lst_sku['sku_channel']);
			$lst_skus[$key]['status_text'] = StringBiz::getSkuStatusRevenueStatusText($lst_sku['status']);
		}
		
		
		

		// 总记录数
		$totalCount = M('sku')->where($condition_array)->count();
		
		// 总页数
		$totalPage = self::caculateTotalPage($totalCount, $pageSize);
		// 当前页记录数
		$currentCount = $lst_skus . $pageSize;
		// 下一页页码
		$nextIndex = $index + 1;
		// 上一页页码 如果是第一页，页码仍然为1
		$preIndex = 1;
		if ($index > 1) {
			$preIndex = $index - 1;
		}
		
		$resultMap = array(
				'totalCount' => $totalCount,
				'totalPage' => $totalPage,
				'currentPage' => $currentCount,
				'nextIndex' => $nextIndex,
				'preIndex' => $preIndex,
				'index' => $index,
				'datas' => $lst_skus
		);
		
	
		return $resultMap;
	
	}
	
	
	public  static function  queryProductById($product_id){
		$product_en = M('product')->where("product_id='%s'", $product_id)->find();
		//成品 && 电视机
		if($product_en['product_type'] == 1 && $product_en['product_group'] == 1){
			$product_en['invoice_title'] = ('o2o云推广' . $product_en['product_size'] . '吋智能电视');
		}else{
			$product_en['invoice_title'] = $product_en['product_name'];
		}
		$resultMap = array(
				'product' =>$product_en,
		);
		return $resultMap;
	}
	/**
	 * Description: 弹出框查询产品
	 * @version 2016年12月9日 下午3:53:45 
	 * @author lin.yujiao@whaley.cn
	 */
	public static function  queryProductInfo($product_type, $product_name, $material_no , $material_desc){
		//$product_id = I('product_id');
		//$product_name = I('product_name');
		//$material_no= I('material_no');
		 
		$con = array();
		/* if (!empty($product_id)) {
			array_push($con, "product_id like '%$product_id%'");
		} */
		if (!empty($product_name)) {
			array_push($con, "product_name like '%$product_name%'");
		}
		if (!empty($material_no)) {
		 	array_push($con, "material_no = '$material_no'");
		} 
		if (!empty($material_desc)) {
			array_push($con, "material_desc like '%$material_desc%'");
		}
		if(!empty($product_type)){
			array_push($con, "product_type = '$product_type'");
		}else{
			//可查询成品与备件
			array_push($con, "(product_type = 1 or product_type =  2)");
		}
		//array_push($con, "product_type = 1  and status =1 ");
		array_push($con, "status =1 ");
		return M('product')->where(join(" AND ", $con))->select();
	}
	public static function generateSkuId(){
		$seq = 10000000 + CommonBiz::getNewSeq('sku_id');
		return 'SK'.$seq;
	}
	
	public static function generateProductId(){
		$seq = 10000000 + CommonBiz::getNewSeq('product_id');
		return 'PD'.$seq;
	}

	public static function checkProduct($product_id, $sku_channel , $sku_id)
	{
		if(!empty($sku_id)){
			$product_en = M('sku')->where("product_id='%s' and sku_channel = '%s' and sku_id <> '%s'", $product_id , $sku_channel ,$sku_id )->find();
		}else{
			$product_en = M('sku')->where("product_id='%s' and sku_channel = '%s'", $product_id , $sku_channel)->find();
		}

		if(!is_null($product_en)){
			return '已经存在';
		}
	}
	/**
	 * Description: 获取VIP会员服务
	 * @version 2016年11月8日 下午2:51:44 
	 * @author lin.yujiao@whaley.cn
	 */
	public static function getVipBagLst(){
		return M('product_split')->where("is_vip_bag = 1")->select();
	}
	/**
	 * Description: 获取相关产品信息
	 * @version 2016年11月8日 下午2:51:44 
	 * @author lin.yujiao@whaley.cn
	 */
	public static function getSplitInfoBySku($sku_id){
		$condition_array['sku_id'] = array('eq', $sku_id);
		$lst_skus = M('product_split')->field()->where($condition_array)->find();
		return $lst_skus;
	}
	/**
	 * Description: 获取发票需要的产品尺寸
	 * @version 2016年11月8日 下午4:18:01 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $product_id
	 */
	public static function getInvProductSize($product_id){
		$sql = "SELECT  CONCAT(t.product_size ,'吋') AS size FROM product t WHERE product_type = 1 and product_id  = '$product_id'";
		$product_lst = M('')->query($sql);
		if(sizeof($product_lst) > 0){
			return $product_lst[0]['size'];
		}
		return '';
	}
	/**
	 * Description: 共通填充
	 * @version 2016年11月8日 下午4:37:44 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $product_split
	 * @param unknown $split_en
	 */
	private static function fillProductSplit(&$product_split,$split_en,$sku_en){
		$product_split['sku_id'] = $split_en['sku_id'];
		$product_split['invoice_name'] = $split_en['invoice_name'];
		//电视机固定税率为0.17 固定单位为台
		$product_split['unit'] = '台';
		$product_split['tax'] = 0.17;
		$product_split['invoice_title'] = $split_en['invoice_title'];
		$product_split['product_id'] = $sku_en['product_id'];
		$product_split['product_size'] = self::getInvProductSize($sku_en['product_id']);
		$product_split['referent_id'] = $split_en['referent_id'];
	}
	/**
	 * Description: 新增产品扩展信息
	 * @version 2016年11月8日 下午4:37:53 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $sku_en
	 */
	public static function addProductSplit($split_en,$sku_en){
		$product_split = array();
		$product_split['id'] = self::generateSplitId();
		$login_user = cookie ( 'login_user_id' );
		$product_split ['create_date'] = date("y-m-d H:i:s", time());
		$product_split ['create_user'] = $login_user;
		self::fillProductSplit($product_split, $split_en, $sku_en);
		$success_count =  M('product_split')->add($product_split);
	}
	/**
	 * Description: 更新产品扩展信息
	 * @version 2016年11月8日 下午4:38:07 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $sku_en
	 */
	public static function updateProductSplit($split_en,$sku_en,$id){
		$product_split = array();
		$product_split['id'] = $id;
		$login_user = cookie ( 'login_user_id' );
		$product_split ['update_date'] = date("y-m-d H:i:s", time());
		$product_split ['update_user'] = $login_user;
		self::fillProductSplit($product_split, $split_en, $sku_en);
		$success_count = M('product_split')->where('id="%s"', array($id))->save($product_split);
	}
	/**
	 * 创建产品扩展信息ID
	 */
	public static function generateSplitId(){
		$seq = CommonBiz::getNewSeq('split_id') + 10000000;
		return 'PST' . $seq;
	}
}